<template>
  <div class="register-container">
    <div class="register-box">
      <h2 class="title">用户注册</h2>
      <el-form :model="registerForm" status-icon :rules="registerRules" ref="registerForm" label-width="0" class="register-form">
        <el-form-item prop="username">
          <el-input 
            prefix-icon="el-icon-user"
            v-model="registerForm.username" 
            placeholder="请输入用户名"
            clearable>
          </el-input>
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input 
            prefix-icon="el-icon-lock"
            v-model="registerForm.password" 
            type="password" 
            placeholder="请输入密码"
            clearable>
          </el-input>
        </el-form-item>
        
        <el-form-item prop="confirmPassword">
          <el-input 
            prefix-icon="el-icon-lock"
            v-model="registerForm.confirmPassword" 
            type="password" 
            placeholder="请确认密码"
            clearable
            @keyup.enter.native="handleRegister">
          </el-input>
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" class="register-button" @click="handleRegister" :loading="loading">注册</el-button>
        </el-form-item>
        
        <div class="login-link">
          <router-link to="/">已有账号？立即登录</router-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { userApi } from '../utils/api'

export default {
  name: 'Register',
  data() {
    // 密码一致性检查
    const validateConfirmPassword = (rule, value, callback) => {
      if (value !== this.registerForm.password) {
        callback(new Error('两次输入密码不一致'))
      } else {
        callback()
      }
    }
    
    return {
      registerForm: {
        username: '',
        password: '',
        confirmPassword: ''
      },
      registerRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { validator: validateConfirmPassword, trigger: 'blur' }
        ]
      },
      loading: false
    }
  },
  methods: {
    handleRegister() {
      this.$refs.registerForm.validate(async (valid) => {
        if (valid) {
          this.loading = true
          try {
            await userApi.register({
              username: this.registerForm.username,
              password: this.registerForm.password
            })
            
            this.$message.success('注册成功，请登录')
            this.$router.push('/')
          } catch (error) {
            console.error('注册失败:', error)
          } finally {
            this.loading = false
          }
        }
      })
    }
  }
}
</script>

<style scoped>
.register-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f5f7f9;
}

.register-box {
  width: 400px;
  padding: 30px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.title {
  text-align: center;
  margin-bottom: 30px;
  color: #409EFF;
}

.register-form {
  margin-top: 20px;
}

.register-button {
  width: 100%;
}

.login-link {
  text-align: center;
  margin-top: 15px;
  font-size: 14px;
}
</style> 